import storage from '@/util/storage'
import md5 from '@/util/md5'
// #ifdef H5
import { jsonp } from 'vue-jsonp'
// #endif

const state = {
	domainPrefix: null,
	orderPreviewGoodsList: [],
	locationInfo: {},
	isLoading: false,

	isInnerFrame: false,
	msgInfo: {
		show: false,
		content: ''
	},

	netOver: false,
	sortCloseState: true
}

const mutations = {
	SET_INNER_FRAME(state, data) {
		state.isInnerFrame = data
	},
	// 设置店铺域名前缀
	SET_DOMAIN(state, data) {
		// H5页面通过页面url来获取,其他端通过路由参数来获取
		// #ifdef H5
		const prefix = "ch"
		state.domainPrefix = prefix
		// #endif

		// #ifndef H5
		// const pages = getCurrentPages();
		// const page = pages[pages.length - 1];
		// const options = (page || {}).options
		// if ((options || {}).domainPrefix) {
		// 	state.domainPrefix = options.domainPrefix
		// } else {
		// 	state.domainPrefix = null
		// }
		state.domainPrefix = storage.get('site').domainPrefix || ""
		// #endif
	},
	// 首页组件公共的跳转方法
	VIEW_SWITCH(state, row) {
		console.log(row)
		if (row.linkUrl === 'SORT_DYNAMIC') {
			uni.navigateTo({
				url: '/pages/index/sort/index?modelId=' + row.modelId + '&componentId=' + row.id + '&modelType=' + row.modelType
			})
			return
		}
		if (row.linkUrl.split('?')[1] === 'from=link') {
			uni.reLaunch({
				url: row.linkUrl
			})
			return
		}
		if (row.linkUrl.split('?')[0] === '/pages/index/home/index') {
			// #ifdef APP-PLUS
			console.log(row.linkUrl)
			uni.reLaunch({
				url: row.linkUrl + '&where=home&token=' + storage.get('token')
			})
			// #endif
			// #ifdef H5
			const url = row.linkUrl.split('?')[1].split('pageUrl=')[1].split('&')[0]
			window.location.href = decodeURIComponent(url)
			// #endif
			return
		}
		if (row.linkUrl.slice(0, 4) === 'http') {
			// #ifdef APP-PLUS
			uni.navigateTo({
				url: '/pages/web/index?src=' + row.linkUrl
			})
			return
			// #endif

			// #ifdef H5
			window.location.href = row.linkUrl
			return
			// #endif
		}

		uni.navigateTo({
			url: row.linkUrl
		})
	},
	// 订单预览数据
	SET_ORDER_PREVIEW_GOODS_LIST(state, data = []) {
		state.orderPreviewGoodsList = data
	},
	// 获取定位
	SET_LOCATION(state, data) {
		state.locationInfo = data
	},

	SET_LOADING(state, data = true) {
		state.isLoading = data
	},

	SET_MSGINFO(state, data) {
		state.msgInfo = data
	},

	SET_NET_OVER(state, data = true) {
		state.netOver = data
	},

	SET_SORT_STATE() {
		state.sortCloseState = false
	}
}

const actions = {
	// 定位
	GetLocation({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			
			uni.getLocation({
				type: 'wgs84',
				success: loca => {
					const key = 'FG4BZ-NHPCO-OEWWI-SSR5Z-7VDBT-XYFWG'
					const location = `${loca.latitude},${loca.longitude}`
					const sig = md5.hex_md5(`/ws/geocoder/v1?key=${key}&location=${location}Ngs6Yif97EL7T0VFah268QzofncR5J3`)
					// #ifdef APP-PLUS
					uni.request({
						url: 'https://apis.map.qq.com/ws/geocoder/v1',
						data: {
							key,
							location,
							sig
						},
						header: {
						},
						success: res => {
							if (res.data.result) {
								commit('SET_LOCATION', res.data.result.ad_info)
								resolve(res.data.result.ad_info)
							} else {
								reject(res.data)
							}
						},
						fail: err => {
							reject(err)
						}
					})
					// #endif
					// #ifdef H5
					jsonp('https://apis.map.qq.com/ws/geocoder/v1', {
						key,
						location,
						sig,
						output: "jsonp"
					}).then(res => {
						if (res.result) {
							commit('SET_LOCATION', res.result.ad_info)
							resolve(res.result.ad_info)
						} else {
							reject(res)
						}
					}).catch(err => {
						reject(err)
					})
					// #endif
				},
				fail: err => {
					uni.showToast({
						icon: 'none',
						title: err,
						duration: 2000
					})
					reject(err)
				}
			})
		})
	},
}

export default {
	state,
	mutations,
	actions
}
